import Vue from 'vue'
import Router from 'vue-router'
import store from './store'

import Home from './pages/home'
import Index from './pages/index'

import TouristList from './pages/touristList'
import TouristDetail from './pages/touristDetail'

import FoodList from './pages/foodList'
import FoodDetail from './pages/foodDetail'

import BmList from "./pages/BmList";
import BmDetail from "./pages/BmDetail";

// 登陆 用户 其他
import Login from './pages/login'


Vue.use(Router);

const router =  new Router({
    routes:[
        {
            path:'/',
            name:'home',
            component: Home,
            redirect:'/index',
            children:[
                {
                    path:'/index',
                    name:'index',
                    component: Index,
                    meta: {needLogin: false}
                },
                {
                    path:'/tourist',
                    name:'TouristList',
                    component: TouristList,
                    meta: {needLogin: false}
                },
                {
                    path:'/tourist/detail/:id',
                    name:'TouristDetail',
                    component: TouristDetail,
                    meta: {needLogin: false}
                },
                {
                    path:'/food',
                    name:'FoodList',
                    component: FoodList,
                    meta: {needLogin: false}
                },
                {
                    path:'/food/detail/:id',
                    name:'FoodDetail',
                    component: FoodDetail,
                    meta: {needLogin: false}
                },
                {
                    path:'/bm',
                    name:'BmList',
                    component: BmList,
                    meta: {needLogin: false}
                },
                {
                    path:'/bm/detail/:id',
                    name:'BmDetail',
                    component: BmDetail,
                    meta: {needLogin: false}
                },
            ]
        },
        {
            path: '/login',
            name: 'login',
            component: Login,
            meta: {needLogin: false}
        }
    ]
});

router.beforeEach((to, from, next) => {
    // 获取store里面的token
    let token = store.state.JWT_TOKEN;
    //判断要去的路由有没有requiresAuth
    if(to.meta.needLogin === true) {

        if(token || to.path === '/login') {
            next();
        } else {
            next({
                path: '/login',
                query: { redirect: to.fullPath }
            });
        }

    }else{
        next();
    }
});

export default router;
